package de.IIIfeed.MoneyPlanner;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.TreeSet;

import org.achartengine.ChartFactory;
import org.achartengine.GraphicalView;
import org.achartengine.chart.PointStyle;
import org.achartengine.model.TimeSeries;
import org.achartengine.model.XYMultipleSeriesDataset;
import org.achartengine.renderer.XYMultipleSeriesRenderer;
import org.achartengine.renderer.XYSeriesRenderer;
import org.kroz.activerecord.ActiveRecordBase;
import org.kroz.activerecord.ActiveRecordException;

import android.app.Activity;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.ViewGroup.LayoutParams;
import android.widget.LinearLayout;

public class Chart extends Activity {
	private XYMultipleSeriesDataset mDataset;
	private XYMultipleSeriesRenderer mRenderer;
	protected GraphicalView mChartView;
	
	protected MoneyPlan mp;
	
	protected static ActiveRecordBase db;
	
	//the last used item
	protected static Item item;
	
	
	protected XYMultipleSeriesDataset getDataset(Map<Date, Float> amounts){
		XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();
		TimeSeries series = new TimeSeries("your money plan ");
		Log.i("getDataset","size="+amounts.size());
		for (Date d : new TreeSet<Date>(amounts.keySet())) {
	      series.add(d, amounts.get(d));
	      Log.i("getDataset","item="+d+"->"+amounts.get(d));
	    }
		 dataset.addSeries(series);
	    return dataset;
	}
	
	private XYMultipleSeriesRenderer getDemoRenderer() {
	    XYMultipleSeriesRenderer renderer = new XYMultipleSeriesRenderer();
	    renderer.setAxisTitleTextSize(16);
	    renderer.setChartTitleTextSize(20);
	    renderer.setLabelsTextSize(15);
	    renderer.setLegendTextSize(15);
	    renderer.setPointSize(5f);
	    renderer.setMargins(new int[] {20, 30, 15, 0});
	    
	    XYSeriesRenderer r = new XYSeriesRenderer();
	    
	    r.setColor(Color.BLUE);
	    r.setPointStyle(PointStyle.SQUARE);
	    r.setFillBelowLine(true);
	    r.setFillBelowLineColor(Color.GREEN);
	    
	    r.setFillPoints(true);
	    renderer.addSeriesRenderer(r);
	    
	    return renderer;
	  }
	
	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.xy_chart);
        
        mRenderer  = getDemoRenderer();
        //dbHelper = DatabaseHelper.getInstance(this);
        //GraphicalView chartView = ChartFactory.getLineChartView(this, getDemoDataset(), getDemoRenderer());
        //LinearLayout chartHolder = (LinearLayout) findViewById(R.id.chart);
    }
    
    @Override
    protected void onResume(){
      super.onResume();
      //do all this onResume so its always uptodate after changes to the db from other activities
      //initial amount and date is stored in sharedPrefs - for easy-ness
      SharedPreferences mPrefs = getSharedPreferences("MoneyPlannerPrefs", 0); 
      float initialAmount = mPrefs.getFloat("initialAmount", 0F);
      Date initialTime;
		try {
			initialTime = (new SimpleDateFormat()).parse(mPrefs.getString("initialTime", ""));
		} catch (ParseException e) {
			initialTime = new Date(); //now
		}
      //items are stored in relational db
      //ArrayList<Item> items = dbHelper.getAllItemsAsArrayList();
      try {
    	  
		  List<Item> items = MoneyPlanner.db.findAll(Item.class);
		  mp = new MoneyPlan(items, initialAmount, initialTime);
		  
		  //build dataset 
		  Map<Date, Float> amounts = mp.getAmountsUntil(new Date(System.currentTimeMillis() + MoneyPlanner.WEEKLY));
		  
		  mDataset = getDataset(amounts);
		  
		  if (mChartView == null) {
		    LinearLayout layout = (LinearLayout) findViewById(R.id.chart);
		    mChartView = ChartFactory.getTimeChartView(this, mDataset, mRenderer,"MMM dd");
		    layout.addView(mChartView, new LayoutParams(LayoutParams.FILL_PARENT,
		        LayoutParams.FILL_PARENT));

		  } else {
		    mChartView.repaint();
		  }
		} catch (ActiveRecordException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    }
}